package edu.its.dreamteam.web.pages;

import java.util.List;

import org.apache.tapestry.ValueEncoder;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.ioc.annotations.Inject;

import edu.its.dreamteam.daf.beans.Formation;
import edu.its.dreamteam.daf.beans.Manager;
import edu.its.dreamteam.daf.beans.Room;
import edu.its.dreamteam.daf.beans.Team;
import edu.its.dreamteam.daf.dao.IFormationDao;
import edu.its.dreamteam.daf.dao.IManagerDao;
import edu.its.dreamteam.daf.dao.IRoomDao;
import edu.its.dreamteam.daf.dao.ITeamDao;
import edu.its.dreamteam.web.base.SimpleBasePage;
import edu.its.dreamteam.web.pages.model.FormationSelectionModel;
import edu.its.dreamteam.web.state.Visit;
import edu.its.dreamteam.web.util.FormationValueEncoder;

public class SignUp extends SimpleBasePage{
	@Inject
	private IManagerDao managerDao;
	@Inject
	private ITeamDao teamDao;
	@Inject
	private IRoomDao roomDao;
	@Inject
	private IFormationDao formationDao;
	
	@Persist
	private Manager manager;
	@Persist
	private Team team;
	@Persist
	private Room room;
	@Persist
	private Formation formation;
	
	@Persist  
	private List<Formation> formationList;
	
	@Component
	private Form createTeamForm;
	
	public Manager getManager() {
		return manager;
	}
	public void setManager(Manager manager) {
		this.manager = manager;
	}
	public Team getTeam() {
		return team;
	}
	public void setTeam(Team team) {
		this.team = team;
	}
	public Room getRoom() {
		return room;
	}
	public void setRoom(Room room) {
		this.room = room;
	}
	public Formation getFormation() {
		return formation;
	}
	public void setFormation(Formation formation) {
		this.formation = formation;
	}
	public IManagerDao getManagerDao() {
		return managerDao;
	}
	public ITeamDao getTeamDao() {
		return teamDao;
	}
	public IRoomDao getRoomDao() {
		return roomDao;
	}	
	
	public IFormationDao getFormationDao() {
		return formationDao;
	}
	public List<Formation> getFormationList() {
		formationList = getFormationDao().queryForList("", "");
		return formationList;
	}
	public FormationSelectionModel getFormationSelectionModel() {		
		return new FormationSelectionModel(getFormationList());
	}
	public ValueEncoder<Formation> getFormationValueEncoder(){
		return new FormationValueEncoder(getFormationList());
	} 
	
	void onPrepare(){
		if(manager == null){
			manager = new Manager();
		}
		if(team == null){
			team = new Team();
		}
		if(formation == null){
			formation = new Formation();
		}
		if(room == null){
			room = new Room();
		}
	}
	
	Object onSuccessFromCreateTeamForm(){
		try{
			getManagerDao().insert(getManager());
		}catch (Exception e) {			
			createTeamForm.recordError("Failed create new manager, Try Again !");
		}
		
		try{
			getTeam().setIdFormation(getFormation().getIdFormation());
			getTeam().setUsername(getManager().getUsername());			
			getTeam().setPoint(175);
			List<Room> roomList = getRoomDao().queryForList("","");
			
			if(roomList.size()==0){
				getRoom().setCurrentTotalTeam(1);
				getRoom().setMaxCapacity(20);
				getRoomDao().insert(getRoom());
				
			}else{
				int size = 0;
				for(Room room : roomList){					
					if(room.getCurrentTotalTeam()<room.getMaxCapacity()){
						getTeam().setIdRoom(room.getIdRoom());
						
					}else{
						size++;
					}
					
					if(size==roomList.size()){
						getRoom().setMaxCapacity(20);
						getRoom().setCurrentTotalTeam(1);
						getRoomDao().insert(getRoom());
						
						List<Room> roomListNew = getRoomDao().queryForList("","");
						Room roomNew = roomListNew.get(roomList.size());
						getTeam().setIdRoom(roomNew.getIdRoom());
					}
				}
			}
			
			getTeamDao().insert(getTeam());
					
		}catch (Exception e) {		
			getManagerDao().delete(getManager());
			createTeamForm.recordError("Failed create new team, Try Again !");
		}
		
		try{
			
			Manager user = (Manager)getManagerDao().findUserId(getManager().getUsername(), getManager().getPassword());	
			if(user!=null){
				Visit visit = getVisit();
				visit.noteLogIn(user);
				
				return Start.class;
			}
		}catch(Exception e) {	
			createTeamForm.recordError("Failed Login !");
		}
		
		
		return null;
	}
	
	void onActionFromClear(){
		setManager(null);
		setTeam(null);
		createTeamForm.clearErrors();
	}	
}
